/**
  ******************************************************************************
  * @file    xxx.c 
  * @author  SD Electronics xxx
  * @version V1.0
  * @date    xx-xx-xx
  * @brief   xxxxxxxxxxxxxxxxxx.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "sd_common.h"


/** 
  * @brief  HALL 
  */ 
ST_HALL_PHY g_stHALLPhyParam;


/** 
  * @brief HALL 
  */ 
ST_HALL_CTRL g_stHALLCtrl = 
{                       
    false,
    &g_stHALLPhyParam,
};  

/**
  * @}
  */

/**
  * @brief  Calcu_hall_num
  * @param  None
  * @note	xxx
  * @retval None
  */
void Calcu_hall_num(PST_HALL_PHY pstHall_Phy, uint32_t H1_level, uint32_t H2_level, uint32_t H3_level)
{
	//check
	if(pstHall_Phy->HALL_CalState == EN_HALL_WITCH)
	{
		if(H1_level == 0 && H2_level > 0 && H3_level > 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H1_CHECK;
		}
		else if(H1_level > 0 && H2_level == 0 && H3_level > 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H2_CHECK;
		}
		else if(H1_level > 0 && H2_level > 0 && H3_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H3_CHECK;
		}
	}
	//H1 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H1_CHECK)
	{
		if(H2_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H1CHECK_STEP_SIMP;
		}
		else if(H3_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H1CHECK_STEP_MORE;
		}
	}
	//H1 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H1CHECK_STEP_SIMP)
	{
		if(H3_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H1CHECK_SIM_SUM;
		}
        else if(H1_level == 0)
        {
			pstHall_Phy->HALL_CalState = EN_HALL_H1_CHECK;
        }
	}
	//H1 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H1CHECK_STEP_MORE)
	{
		if(H2_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H1CHECK_MORE_SUM;
		}
        else if(H1_level == 0)
        {
			pstHall_Phy->HALL_CalState = EN_HALL_H1_CHECK;
        }

	}
	//H1 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H1CHECK_SIM_SUM)
	{
		if(H1_level == 0)
		{
			pstHall_Phy->HALL_CIRCLE ++;
			pstHall_Phy->HALL_CalState = EN_HALL_WITCH;
		}
        else if(H2_level == 0)
        {
			pstHall_Phy->HALL_CalState = EN_HALL_H1CHECK_STEP_SIMP;
        }
	}
	//H1 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H1CHECK_MORE_SUM)
	{
		if(H1_level == 0)
		{
			pstHall_Phy->HALL_CIRCLE ++;
			pstHall_Phy->HALL_CalState = EN_HALL_WITCH;
		}
        else if(H3_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H1CHECK_STEP_MORE;
		}
	}
	
	/*****************HALL2 CHECK****************/
	//H2 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H2_CHECK)
	{
		if(H1_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H2CHECK_STEP_SIMP;
		}
		else if(H3_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H2CHECK_STEP_MORE;
		}
	}
	//H2  Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H2CHECK_STEP_SIMP)
	{
		if(H3_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H2CHECK_SIM_SUM;
		}
        else if(H2_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H2_CHECK;
		}
	}
	//H2 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H2CHECK_STEP_MORE)
	{
        if(H2_level == 0)
        {
            pstHall_Phy->HALL_CalState = EN_HALL_H2_CHECK;
        }
		else if(H1_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H2CHECK_MORE_SUM;
		}
	}
	//H2 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H2CHECK_SIM_SUM)
	{
        if(H2_level == 0)
		{
			pstHall_Phy->HALL_CIRCLE ++;
			pstHall_Phy->HALL_CalState = EN_HALL_WITCH;
		}
        
	}
    //H2 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H2CHECK_MORE_SUM)
	{
        if(H2_level == 0)
		{
			pstHall_Phy->HALL_CIRCLE ++;
			pstHall_Phy->HALL_CalState = EN_HALL_WITCH;
		}
        else if(H3_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H2CHECK_STEP_MORE;
		}
        
	}
	
	/*****************HALL3 CHECK****************/
	//H3 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H3_CHECK)
	{
		if(H1_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H3CHECK_STEP_SIMP;
		}
		else if(H2_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H3CHECK_STEP_MORE;
		}
	}
	//H3 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H3CHECK_STEP_SIMP)
	{
		if(H2_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H3CHECK_SIM_SUM;
		}
        else if(H3_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H3_CHECK;
		}
	}
	//H3 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H3CHECK_STEP_MORE)
	{
		if(H1_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H3CHECK_MORE_SUM;
		}
        else if(H3_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H3_CHECK;
		}
	}
	//H3 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H3CHECK_MORE_SUM)
	{
		if(H3_level == 0)
		{
			pstHall_Phy->HALL_CIRCLE ++;
			pstHall_Phy->HALL_CalState = EN_HALL_WITCH;
		}
        else if(H2_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H3CHECK_STEP_MORE;
		}
	}
    //H3 Check
	else if(pstHall_Phy->HALL_CalState == EN_HALL_H3CHECK_SIM_SUM)
	{
		if(H3_level == 0)
		{
			pstHall_Phy->HALL_CIRCLE ++;
			pstHall_Phy->HALL_CalState = EN_HALL_WITCH;
		}
        else if(H1_level == 0)
		{
			pstHall_Phy->HALL_CalState = EN_HALL_H3CHECK_STEP_SIMP;
		}
	}
}

/**
  * @brief  SDInitHALL
  * @note   xxx
  * @param  None
  * @retval None
  */
void SDInitHALL(PST_HALL_PHY pstHALLCntPhy)
{
    pmu_set_port_mux(GPIO_PORT_A,GPIO_BIT_0,PMU_PORT_MUX_GPIO);//HALL2
    pmu_set_port_mux(GPIO_PORT_A,GPIO_BIT_1,PMU_PORT_MUX_GPIO);//HALL1
    pmu_set_port_mux(GPIO_PORT_A,GPIO_BIT_4,PMU_PORT_MUX_GPIO);//HALL3

    pmu_set_pin_to_PMU(GPIO_PORT_A,BIT(0)|BIT(1)|BIT(4));
    pmu_set_pin_dir(GPIO_PORT_A,BIT(0)|BIT(1)|BIT(4), GPIO_DIR_IN);
    pmu_set_pin_pull(GPIO_PORT_A,BIT(0)|BIT(1)|BIT(4), true);
    printf("usr: sd HALL init.\r\n");
}

/**
  * @brief  HALLOpen
  * @note   xxx
  * @param  None
  * @retval None
  */
static long HALLOpen(void *pvHandle, unsigned long ulOption)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;

    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_HALL_CTRL pstHALLCtrl = (PST_HALL_CTRL)pstDevManage->pvData;
        if(false == pstHALLCtrl->bInitFlag)
        {
			/* HALL初始化 */
            SDInitHALL(pstHALLCtrl->pstHALLInfo);
            pstHALLCtrl->bInitFlag = true;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;
}  

/**
  * @brief  HALLRead
  * @note   xxx
  * @param  None
  * @retval None
  */
static long HALLRead(void *pvHandle, unsigned long ulOpertion, unsigned char *pucDataBuf, int* ulDataSize)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_HALL_CTRL pstHALLCtrl = (PST_HALL_CTRL)pstDevManage->pvData;
        //
        if(true == pstHALLCtrl->bInitFlag)
        {
            pstHALLCtrl->pstHALLInfo->HALL1_Flag = pmu_get_gpio_value(GPIO_PORT_A, GPIO_BIT_1);//hall1
            pstHALLCtrl->pstHALLInfo->HALL2_Flag = pmu_get_gpio_value(GPIO_PORT_A, GPIO_BIT_0);//hall2
            pstHALLCtrl->pstHALLInfo->HALL3_Flag = pmu_get_gpio_value(GPIO_PORT_A, GPIO_BIT_4);//hall3
            Calcu_hall_num(pstHALLCtrl->pstHALLInfo, pstHALLCtrl->pstHALLInfo->HALL1_Flag, pstHALLCtrl->pstHALLInfo->HALL2_Flag, pstHALLCtrl->pstHALLInfo->HALL3_Flag);
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;    
}

/**
  * @brief  HALLWrite
  * @note   xxx
  * @param  None
  * @retval None
  */
static long HALLWrite(void *pvHandle, unsigned long ulOpertion, unsigned char *pucDataBuf, unsigned long ulDataLen)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_HALL_CTRL pstHALLCtrl = (PST_HALL_CTRL)pstDevManage->pvData;
        //
        if(true == pstHALLCtrl->bInitFlag)
        {
           ;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;
}

/**
  * @brief  HALLControl
  * @note   xxx
  * @param  None
  * @retval None
  */
static long HALLControl (void *pvHandle,unsigned long ulOpertion, unsigned long ulControlCmd, void *pvd)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    { 
        PST_HALL_CTRL pstHALLCtrl = (PST_HALL_CTRL)pstDevManage->pvData;
        /* ??????? */
        if(true == pstHALLCtrl->bInitFlag)
        {
          ;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;             
}

/**
  * @brief  HALLClose
  * @note   xxx
  * @param  None
  * @retval None
  */
static long HALLClose (void *pvHandle)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;     
}



/**
  * @brief  g_stHALLCtlFUN
  * @note  xxx
  * @param  None
  * @retval None
  */
ST_CTR_FUN g_stHALLCtlFUN = 
{
    HALLOpen,		//??
    HALLRead,		//?
    HALLWrite,		//?
    HALLControl,	//??
    HALLClose    	//??
};


/******************* (C) COPYRIGHT 2011 ShenZhenShiding electronics **************************/
